/*
 * Copyright (C) 2016 MarkZhai (http://zhaiyifan.cn).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package utils;


import android.databinding.BindingAdapter;
import android.databinding.BindingConversion;
import android.databinding.InverseBindingAdapter;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import temp.SuperFrameLayout;


import java.text.SimpleDateFormat;
import java.util.Date;




public class BindingAdapterUtil {


    @BindingAdapter("url")
    public static void setImgURL(ImageView view, String url) {
        Glide.with(view.getContext()).load(url).into(view);

    }

    @BindingAdapter("text_long")
    public static void setText(TextView view, long v) {
//        if (!view.getText().toString().equals(v)){
//            view.setText(v.toString());
//        }

        view.setText(String.valueOf(v));
    }

    @BindingAdapter("text_float")
    public static void setText(TextView view, float v) {
//        if (!view.getText().toString().equals(v)){
//            view.setText(v.toString());
//        }

        view.setText(String.valueOf(v));
    }
    @BindingAdapter("text_int")
    public static void setText(TextView view, int v) {
//        if (!view.getText().toString().equals(v)){
//            view.setText(v.toString());
//        }

        view.setText(String.valueOf(v));
    }

//    @InverseBindingAdapter(attribute = "text", event = "android:textAttrChanged")
//    public static String getText(TextView view) throws NumberFormatException {
//        return  view.getText().toString();
//
//    }


    @BindingAdapter("text_int")
    public static void setIntValue(EditText view, int v) {
        view.setText(String.valueOf(v));
    }

    @InverseBindingAdapter(attribute = "text_int", event = "android:textAttrChanged")
    public static int getIntValue(EditText view) throws NumberFormatException {
        String string = view.getText().toString();
        if (TextUtils.isEmpty(string)) {
            return 0;
        }
        try {
            return Integer.parseInt(string);
        } catch (Exception e) {
            return  string.startsWith("-")?Integer.MIN_VALUE:Integer.MAX_VALUE;
        }


    }

    @BindingAdapter("text_float")
    public static void setFloatValue(EditText view, float v) {
        view.setText(String.valueOf(v));
    }

    @InverseBindingAdapter(attribute = "text_float", event = "android:textAttrChanged")
    public static float getFloatValue(EditText view) throws NumberFormatException {
        String string = view.getText().toString();
        if (TextUtils.isEmpty(string)) {
            return 0.0f;
        }
        try {
            return Float.parseFloat(string);
        } catch (Exception e) {
            return  string.startsWith("-")?Float.MIN_VALUE:Float.MAX_VALUE;
        }


    }

    @BindingAdapter("text_long")
    public static void setLongValue(EditText view, long v) {
        view.setText(String.valueOf(v));
    }

    @InverseBindingAdapter(attribute = "text_long", event = "android:textAttrChanged")
    public static long getLongValue(EditText view) throws NumberFormatException {
        String string = view.getText().toString();
        if (TextUtils.isEmpty(string)) {
            return 0;
        }
        try {
            return Long.parseLong(string);
        } catch (Exception e) {

            return  string.startsWith("-")?Long.MIN_VALUE:Long.MAX_VALUE;
        }


    }

    @BindingAdapter("text_double")
    public static void setDoubleValue(EditText view, double v) {
        view.setText(String.valueOf(v));
    }

    @InverseBindingAdapter(attribute = "text_double", event = "android:textAttrChanged")
    public static double getDoubleValue(EditText view) throws NumberFormatException {
        String string = view.getText().toString();
        if (TextUtils.isEmpty(string)) {
            return 0;
        }
        try {
            return Double.parseDouble(string);
        } catch (Exception e) {

            return  string.startsWith("-")?Double.MIN_VALUE:Double.MAX_VALUE;
        }


    }

    @BindingAdapter("text_boolean")
    public static void setBooleanValue(EditText view, boolean v) {
        view.setText(String.valueOf(v));
    }

    @InverseBindingAdapter(attribute = "text_boolean", event = "android:textAttrChanged")
    public static boolean getBooleanValue(EditText view) throws NumberFormatException {
        String string = view.getText().toString();
        if (TextUtils.isEmpty(string)) {
            return false;
        }
        try {
            return Boolean.parseBoolean(string);
        } catch (Exception e) {
            return  false;
        }


    }

    @BindingConversion
    public static String convertDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    @BindingConversion
    public static String convertString(int i) {

        return String.valueOf(i);

    }



    @BindingAdapter("state")
    public static void setState(SuperFrameLayout view, int state) {
        view.setState(state);
    }






    @InverseBindingAdapter(attribute = "checked", event = "android:checkedAttrChanged")
    public static boolean getchecked(CheckBox view)  {
        return view.isChecked();



    }
}
